package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 韶关市侨联
 * @author KK
 *
 */
public class SgqlNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://www.sgql.org/news/[A-Z]*/[0-9]*/[0-9]*/[a-zA-Z0-9]*.html",
	};	
	
	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}		
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();	
						
		news.setUrl(url);		
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("title").text();	
		if (title!=null)title=title.replace("韶关市归国华侨联合会--", "");
		if (StringUtils.isNullOrEmpty(title)){
			title =doc.select("table.show td b").text();	
		}
		
		content=HtmlCleaner.getContentHtml(url,doc.select("div.Section0"));			
		date=DateUtils.matchDate(doc.select("td:contains(发布日期：)").text());
		if (date==null){
//			date=DateUtils.matchDate(doc.select("div.nrbq").text());
		}

		if (StringUtils.isNotBlank(title) && date!=null){			
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setDate(date);	
			news.setType(1);	
		}
		return news;
	}
	
	public static void main(String[] args) {
		SgqlNewsAnalyse analyse=new SgqlNewsAnalyse();
		String url="http://www.sgql.org/news/SQLDT/2013/115/13115154038D8A298E66AA3CBF2840K.html";
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
